package forestsimulatorp3.model.beings.abstrakt;


import forestsimulatorp3.model.beings.Environment;
import forestsimulatorp3.model.position.Square;
import forestsimulatorp3.model.simulator.Time;

/**
 * Represents a living being.
 * @author dielson
 *
 */
public abstract class LivingBeing {

	// Attributes --------------------------------------------------------------

	private long id;
	private String speciesName;
	private long bornCycle;
	private Environment environment;
	private int lifePoints;

	private Square position;

	// Constructors ------------------------------------------------------------

	public LivingBeing(long id, String speciesName, Environment envirionment, int lifePoints, Square position) {

		setId(id);
		setSpeciesname(speciesName);
		setEnvironment(envirionment);
		setLifePoints(lifePoints);
		setPosition(position);
		this.bornCycle = Time.getCurrentCycle();
		this.environment = envirionment;
	}

	// Public methods ----------------------------------------------------------

	/**
	 * Returns the current age of the living being.
	 */
	public final long getAge() {
		return Time.getCurrentCycle() - bornCycle;
	}

	/**
	 * The animal dies.
	 */
	public void die() {
		environment.removeLivingBeing(this);
		environment.getLogger().notifyAction(this + " morreu.");
	}

	/**
	 * Refreshes the strength of the living being.
	 */
	public abstract void rest();
	// Getters & setters -------------------------------------------------------

	public long getId() {
		return id;
	}

	public final void setId(long id) {
		this.id = id;
	}

	public String getSpeciesName() {
		return speciesName;
	}

	public final void setSpeciesname(String speciesName) {
		this.speciesName = speciesName;
	}

	public int getLifePoints() {
		return lifePoints;
	}

	public final void setLifePoints(int lifePoints) {
		if (lifePoints <= 0) {
			die();
		}
		this.lifePoints = lifePoints;
	}

	public Square getPosition() {
		return position;
	}

	public final void setPosition(Square position) {
		this.position = position;
	}

	public Environment getEnvironment() {
		return environment;
	}

	public final void setEnvironment(Environment environment) {
		if (this.environment == null) {
			this.environment = environment;
		} else {
			this.environment.removeLivingBeing(this);
		}
		environment.addLivingBeing(this);
	}

	// Overrides String --------------------------------------------------------

	@Override
	public String toString() {
		return speciesName + " " + id;
	}
}
